package cc.shacocloud.luanniao.core.service.oss

/**
 * 抽象的oss 层
 *
 * @author 思追(shaco)
 */
abstract class AbstractObjectStorageService : ObjectStorageService {

    @Volatile
    private var destroy = false

    /**
     * 销毁方法建议子类实现 [internalDestroy]
     */
    override fun destroy() {
        this.destroy = true

        try {
            internalDestroy()
        } catch (e: Exception) {
            throw RuntimeException("销毁对象存储服务 $this 发生例外！", e)
        }
    }

    /**
     * 内部的销毁方法，提供给子类实现
     */
    @Throws(Exception::class)
    fun internalDestroy() {
    }

    /**
     * 检查当前对象是否继续可用
     */
    protected fun checkAvailable() {
        check(!destroy) { "该对象已经被销毁！" }
    }

    /**
     * 将bucket和key合并为一个路径
     */
    fun destPath(bucket: String, key: String): String {
        return "${bucket.removePrefix("/").removeSuffix("/")}/${key.removePrefix("/")}"
    }
}
